/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bibsys;

import bibSysTray.BibsysTray;
import fastSplash.SplashWindow;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Pardal
 */
public class MainWindow extends javax.swing.JFrame {

    private static final String progName = "BibSys ";
    private JFileChooser fc = new JFileChooser();
    private BufferedReader bibReader;    

    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        initComponents();
        setLocationRelativeTo(null);

        setIcon();

        FileFilter bibFilter = new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }

                if (f.getName().endsWith(".bib")) {
                    return true;
                }

                return false;
            }

            @Override
            public String getDescription() {
                return "Arquivos BibTex (.bib)";
            }
        };
        fc.setFileFilter(bibFilter);
        fc.setAcceptAllFileFilterUsed(true);
    }

    private void setIcon() {
        Image progIcon = new ImageIcon(BibsysTray.class.getResource("book_icon.gif"), "BibSys").getImage();
        this.setIconImage(progIcon);
    }

    private void readPreview() {
        try{            
            bibView.read(bibReader, null);
            bibReader.close();            
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        bibView = new javax.swing.JTextArea();
        jToolBar1 = new javax.swing.JToolBar();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        parserTool = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu_Arquivo = new javax.swing.JMenu();
        menu_open = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        menu_save_as = new javax.swing.JMenuItem();
        menu_exit = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenu_Sobre = new javax.swing.JMenu();
        about = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BibSys");

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(jTree1);

        bibView.setColumns(20);
        bibView.setRows(5);
        jScrollPane2.setViewportView(bibView);

        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bibSysTray/doc_new_icon.gif"))); // NOI18N
        jButton3.setToolTipText("Novo");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton3);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bibSysTray/doc_import_icon.gif"))); // NOI18N
        jButton1.setToolTipText("Abrir");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bibSysTray/doc_export_icon.gif"))); // NOI18N
        jButton2.setToolTipText("Salvar");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton2);

        parserTool.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bibSysTray/cogs_icon.gif"))); // NOI18N
        parserTool.setToolTipText("Formatar");
        parserTool.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parserToolActionPerformed(evt);
            }
        });
        jToolBar1.add(parserTool);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Visualizar .bib", jPanel1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 586, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 617, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Visualizar referências", jPanel2);

        jMenu_Arquivo.setText("Arquivo");

        menu_open.setText("Abrir");
        menu_open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_openActionPerformed(evt);
            }
        });
        jMenu_Arquivo.add(menu_open);

        jMenuItem2.setText("Salvar");
        jMenu_Arquivo.add(jMenuItem2);

        menu_save_as.setText("Salvar Como ...");
        menu_save_as.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_save_asActionPerformed(evt);
            }
        });
        jMenu_Arquivo.add(menu_save_as);

        menu_exit.setText("Sair");
        menu_exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_exitActionPerformed(evt);
            }
        });
        jMenu_Arquivo.add(menu_exit);

        jMenuBar1.add(jMenu_Arquivo);

        jMenu1.setText("Opções");
        jMenuBar1.add(jMenu1);

        jMenu_Sobre.setText("Ajuda");

        about.setText("Sobre");
        about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutActionPerformed(evt);
            }
        });
        jMenu_Sobre.add(about);

        jMenuBar1.add(jMenu_Sobre);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed
        SplashWindow.splash(SplashWindow.class.getResource("livros.jpg"));
    }//GEN-LAST:event_aboutActionPerformed

    private void menu_exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_exitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_menu_exitActionPerformed

    private void menu_openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_openActionPerformed
        int status = fc.showOpenDialog(this);
        if (status == JFileChooser.APPROVE_OPTION) {
            File bibFile = fc.getSelectedFile();
            try {
                bibReader = new BufferedReader(new FileReader(bibFile));
                this.setTitle(progName + bibFile.getName());
                readPreview();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null,
                        ex.getMessage(), progName, JOptionPane.ERROR_MESSAGE);
                this.setTitle(progName);
            }

        }
    }//GEN-LAST:event_menu_openActionPerformed

    private void menu_save_asActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_save_asActionPerformed
        fc.showSaveDialog(this);
    }//GEN-LAST:event_menu_save_asActionPerformed

    private void parserToolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parserToolActionPerformed
       List<Publicacao> publicacoes = BibSys.pasrseBib(bibView.getText());
       
    }//GEN-LAST:event_parserToolActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem about;
    private javax.swing.JTextArea bibView;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenu jMenu_Arquivo;
    private javax.swing.JMenu jMenu_Sobre;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTree jTree1;
    private javax.swing.JMenuItem menu_exit;
    private javax.swing.JMenuItem menu_open;
    private javax.swing.JMenuItem menu_save_as;
    private javax.swing.JButton parserTool;
    // End of variables declaration//GEN-END:variables
}
